import axios from "axios";
import {ElNotification} from 'element-plus'
import {useUserInfoStore} from "@/stores/user_info";
import {ElLoading} from 'element-plus'

let fileRequest = axios.create({
    //@ts-ignore
    baseURL: import.meta.env.VITE_APP_BASE_API,//基础路径，前面会拼接/dev-api
    timeout: 5000,
})


fileRequest.interceptors.request.use((config) => {
    const userInfoStore = useUserInfoStore()
    if (userInfoStore.token) {
        config.headers.Authorization = userInfoStore.token
    }
    return config;
})

fileRequest.interceptors.response.use((res) => {
    return new Promise((resolve, reject) => {
        const data = res.data
        if (data.code == 200) {
            resolve(data)
        } else if (data.code == 203) {
            ElNotification({
                message: data.msg,
                duration: 1500,
                type: 'error',
            })
            reject(data)
        } else {
            reject(data)
        }
    })
}, (error) => {
    ElNotification({
        message: "网络错误！请稍后重试",
        duration: 1500,
        type: 'error',
    })
})

export default fileRequest;